home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2001 December / pcwk12201b.iso / Wersje pelne i specjalne / Winamp 2.77 i 3.0beta / wasabi-sdk_beta1.exe / studio / common / tlist.h < prev    next >
C/C++ Source or Header  |  2001-10-08  |  4KB  |  186 lines

  1. /*
  2.  
  3.   Nullsoft WASABI Source File License
  4.  
  5.   Copyright 1999-2001 Nullsoft, Inc.
  6.  
  7.     This software is provided 'as-is', without any express or implied
  8.     warranty.  In no event will the authors be held liable for any damages
  9.     arising from the use of this software.
  10.  
  11.     Permission is granted to anyone to use this software for any purpose,
  12.     including commercial applications, and to alter it and redistribute it
  13.     freely, subject to the following restrictions:
  14.  
  15.     1. The origin of this software must not be misrepresented; you must not
  16.        claim that you wrote the original software. If you use this software
  17.        in a product, an acknowledgment in the product documentation would be
  18.        appreciated but is not required.
  19.     2. Altered source versions must be plainly marked as such, and must not be
  20.        misrepresented as being the original software.
  21.     3. This notice may not be removed or altered from any source distribution.
  22.  
  23.  
  24.   Brennan Underwood
  25.   brennan@nullsoft.com
  26.  
  27. */
  28.  
  29. //PORTABLE
  30. #ifndef _TLIST_H
  31. #define _TLIST_H
  32.  
  33. #include "common.h"
  34. #include "std.h"
  35. #include "named.h"
  36.  
  37. /*
  38.  
  39. A generic std::vector wannabe...
  40.  
  41. NOTE: you must only use this with basic types! constructors and destructors
  42. will not be called.
  43.  
  44. */
  45.  
  46. #include "../studio/assert.h"
  47.  
  48. #define TLIST_INCREMENT 32
  49.  
  50. namespace wasabi {
  51. template<class T>
  52. class COMEXP TList {
  53. public:
  54.   TList() {
  55.     nitems = 0;
  56.     nslots = 0;
  57.     items = NULL;
  58.   }
  59.   virtual ~TList() {
  60.     FREE(items);
  61.   }
  62.  
  63.   int getNumItems() const { return nitems; };
  64.  
  65.   // CAN'T PROVIDE BOUNDS CHECKING!!! (except by ASSERT() :( )
  66.   T enumItem(int n) const {
  67.     ASSERT(items != NULL);
  68.     ASSERT(n >= 0 && n < nitems);
  69.     return items[n];
  70.   }
  71.   T operator[](int n) const { return enumItem(n); }
  72.  
  73.   T getFirst() const { return enumItem(0); }
  74.   T getLast() const { return enumItem(getNumItems()-1); }
  75.  
  76.   T *enumRef(int n) const {
  77.     ASSERT(items != NULL);
  78.     ASSERT(n >= 0 && n < nitems);
  79.     return items+n;
  80.   }
  81.  
  82.   T *addItem(T item) {
  83.     ASSERT(nitems <= nslots);
  84.     if (items == NULL) {
  85.       nslots = TLIST_INCREMENT;
  86.       items = (T *)MALLOC(sizeof(T) * nslots);
  87.       nitems = 0;
  88.     } else if (nslots == nitems) {    // time to expand
  89.       T *newitems;
  90.       nslots += TLIST_INCREMENT;
  91.       newitems = (T *)MALLOC(sizeof(T) * nslots);
  92.       MEMCPY(newitems, items, nitems * sizeof(T));
  93.       FREE(items);
  94.       items = newitems;
  95.     }
  96.     items[nitems++] = item;
  97.  
  98.     return &items[nitems-1];
  99.   }
  100.  
  101.   T *insertItem(T item, int pos) { // position to insert
  102.     ASSERT(pos >= 0 && pos <= nitems);
  103.     T *tmp=addItem(item);
  104.     int n=nitems-1;
  105.     while (n > pos) {
  106.       items[n]=items[n-1];
  107.       n--;
  108.     }
  109.     tmp=items+pos;
  110.     *tmp=item;
  111.     return tmp;
  112.   }
  113.  
  114.   T *replaceItemByPos(T item, int pos) {
  115.     ASSERT(items != NULL);
  116.     ASSERT(nitems != 0);
  117.     ASSERT(pos >= 0 && pos < nitems);
  118.     items[pos]=item;
  119.     return &items[pos];
  120.   }
  121.  
  122.   void setItem(int pos, T newval) {
  123.     if (pos < 0) return;
  124.     if (pos >= nitems) return;
  125.     items[pos] = newval;
  126.   }
  127.  
  128.   int delItem(T item) {
  129.     int c = 0;
  130.  
  131.     ASSERT(items != NULL);
  132.     ASSERT(nitems != 0);
  133.  
  134.     T *src = items;
  135.     T *dst = items;
  136.     for (int i = 0; i < nitems; i++) {
  137.       if (*src != item) {
  138.         *dst++ = *src;
  139.         c++;
  140.       }
  141.       src++;
  142.     }
  143.     nitems -= c;
  144.  
  145.     return c;
  146.   }
  147.  
  148.   int delByPos(int pos) {
  149.     if (pos < 0 || pos >= nitems) return 0;
  150.     --nitems;
  151.     if (pos == nitems) return 1;    // last one? nothing to copy over
  152.     MEMCPY(items+pos, items+(pos+1), sizeof(T)*(nitems-pos));  // CT:not (nitems-(pos+1)) as nitems has been decremented earlier
  153.     return 1;
  154.   }
  155.  
  156.   void removeAll() {
  157.     FREE(items); items = NULL;
  158.     nitems = 0;
  159.     nslots = 0;
  160.   }
  161.  
  162.   void freeAll() {
  163.     for (int i = 0; i < nitems; i++)
  164.       FREE(items[i]);
  165.     removeAll();
  166.   }
  167.  
  168.   int getItemPos(T it) const {
  169.     for (int i=0;i<getNumItems();i++)
  170.       if(!MEMCMP(&items[i],&it,sizeof(T))) return i; // CT     if (items[i] == it) return i;
  171.     return -1;
  172.   }
  173.  
  174. private:
  175.   int nitems, nslots;
  176.   T *items;
  177. };
  178. }
  179.  
  180. namespace wasabi {
  181. template<class T>
  182. class TListNamed : public TList<T>, public Named {};
  183. }  // namespace wasabi
  184.  
  185. #endif
  186.